package de.fhb.itunes.controller.web;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import de.fhb.itunes.repository.DataAccess.DAO_Episode;
import de.fhb.itunes.repository.DataAccess.DAO_MediaFile;
import de.fhb.itunes.repository.DataAccess.DAO_Musik;
import de.fhb.itunes.repository.DataAccess.IDAO_MediaFile;
import de.fhb.itunes.repository.DataAccess.IDAO_Musik;
import de.fhb.itunes.repository.data.MedienFile_VO;
import de.fhb.itunes.repository.data.Musik_VO;

/**
 * Servlet implementation class Download
 */
@WebServlet("/Download")
public class Download extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Download() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			boolean isLoggedIn;
			if (objtmp != null) {
				isLoggedIn = (Boolean) objtmp;
			} else {
				isLoggedIn = false;
			}
			if (isLoggedIn) {
				System.out.println("im Download");
				// logik anfang
				Enumeration en = request.getParameterNames();
				int id = -1;
				while (en.hasMoreElements()) {
					String paramName = (String) en.nextElement();
					String paraValue = request.getParameter(paramName);
					if (paramName.equalsIgnoreCase("id"))
						id = Integer.parseInt(paraValue);
				}
				if (id != -1) {
					IDAO_MediaFile verwaltung = new DAO_MediaFile();
					MedienFile_VO titel = verwaltung.getByID(id);
					if (titel != null) {
						request.setAttribute("pfad", titel.getPfad());
						nextJSP = "/download.jsp";
					} else {
						nextJSP = "/index.jsp";
					}
				} else {
					nextJSP = "/index.jsp";
				}
				// logik ende
			}

		}
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			boolean isLoggedIn;
			if (objtmp != null) {
				isLoggedIn = (Boolean) objtmp;
			} else {
				isLoggedIn = false;
			}
			if (isLoggedIn) {
				// logik anfang
				// logik ende
			}

		}
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

}
